Operator SDK also has support for Ansible and Helm operators, which make it easy to write operators without having to learn Go and if you already have experience with Ansible or Helm
Operator SDK includes integrations with the Operator Lifecycle Manager (OLM), which is a key component of the Operator Framework that is important to Day 2 cluster operations, like managing a live upgrade of your operator.
Operator SDK includes a scorecard subcommand that helps you understand if your operator follows best practices.
Operator SDK includes an e2e testing framework that simplifies testing your operator against an actual cluster.
Kubebuilder includes an envtest package that allows operator developers to run simple tests with a standalone etcd and apiserver.
Kubebuilder scaffolds a Makefile to assist users in operator tasks (build, test, run, code generation, etc.); Operator SDK is currently using built-in subcommands. Each has pros and cons. The SDK team will likely be migrating to a Makefile-based approach in the future.
Kubebuilder uses Kustomize to build deployment manifests; Operator SDK uses static files with placeholders.
Kubebuilder has recently improved its support for admission and CRD conversion webhooks, which has not yet made it into SDK.
Code Generator 是用于实现 Kubernetes 风格 API 类型的 Golang 代码生成器。可以利用该工程自动生成指定K8s 资源的 clientset、informers 和 listers API 接口,本身是位于 Kubernetes 项目中的工具,内部包含了大量的生成器,例如 client-gen,deepcopy-gen,informer-gen,lister-gen 等等。
// SchemeGroupVersion is group version used to register these objects. var SchemeGroupVersion = GroupVersion
// Resource takes an unqualified resource and returns a Group qualified GroupResource funcResource(resource string) schema.GroupResource { return SchemeGroupVersion.WithResource(resource).GroupResource() }
<CRD>_types.go
以 pkg/apis/android/v1/animage_types.go 为例,新增以下 tag 信息
/* Copyright 2022 AKE. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// AnImageSpec defines the desired state of AnImage type AnImageSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file
// Foo is an example field of AnImage. Edit animage_types.go to remove/update Foo string`json:"foo,omitempty"` }
// AnImageStatus defines the observed state of AnImage type AnImageStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file }
// AnImageList contains a list of AnImage type AnImageList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` Items []AnImage `json:"items"` }
#!/usr/bin/env bash # #Copyright 2022 AKE. # #Licensed under the Apache License, Version 2.0 (the "License"); #you may not use this file except in compliance with the License. #You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing, software #distributed under the License is distributed on an "AS IS" BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #See the License for the specific language governing permissions and #limitations under the License.
set -o errexit set -o nounset set -o pipefail # this script expects to be run from the root of the android-operator repo. # Corresponding to go mod init <module>. MODULE=huayun.io/ake/android-operator # Corresponding to kubebuilder create api --group <group> --version <version>. GROUP_VERSION=android:v1 # Generated output package. OUTPUT_PKG=pkg/generated # API package. APIS_PKG=pkg/apis
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. # Sync k8s.io/code-generator to go.mod go mod tidy # Grab code-generator version from go.mod. CODEGEN_VERSION=$(grep 'k8s.io/code-generator' go.mod | awk '{print $2}') CODEGEN_PKG=$(go env GOPATH)/pkg/mod/k8s.io/code-generator@${CODEGEN_VERSION} # Prepare code-generator. if [[ ! -d ${CODEGEN_PKG} ]]; then echo "${CODEGEN_PKG} is missing. Running 'go mod download'." go mod download fi
echo ">> Using ${CODEGEN_PKG}" # code-generator does work with go.mod but makes assumptions about # the project living in `$GOPATH/src`. To work around this and support # any location; create a temporary directory, use this as an output # base, and copy everything back once generated. TEMP_DIR=$(mktemp -d) cleanup() { echo ">> Removing ${TEMP_DIR}" rm -rf "${TEMP_DIR}" } trap "cleanup" EXIT SIGINT
echo ">> Temporary output directory ${TEMP_DIR}"
chmod +x "${CODEGEN_PKG}"/generate-groups.sh # generate the code with: # --output-base because this script should also be able to run inside the vendor dir of # k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir # instead of the $GOPATH directly. For normal projects this can be dropped. #cd"${SCRIPT_ROOT}" "${CODEGEN_PKG}"/generate-groups.sh \ all \ ${MODULE}/${OUTPUT_PKG} \ ${MODULE}/${APIS_PKG} \ ${GROUP_VERSION} \ --output-base "${TEMP_DIR}" \ --go-header-file "${SCRIPT_ROOT}"/hack/boilerplate.go.txt # Copy everything back. cp -a "${TEMP_DIR}/${MODULE}/." "${SCRIPT_ROOT}/"
Makefile
Makefile 中新增 .PHONY,集成构建流程。
1 2 3
.PHONY: update-codegen update-codegen: ## Generate code by code-generator bash ./hack/update-codegen.sh